<?php
class AppController extends Controller {

    public $components = array('Auth', 'Acl', 'Cookie', 'Response');
    public $helpers = array('Html', 'Form', 'Javascript', 'Ajax');
    public $paginate = array('limit'=>10);
    public $uses = array('User', 'Group');
    /* Protected Variable to store current session user data */
    protected $user = array();
    protected $default_user = array(
        'id' => null,
        'type' => null,
        'name' => null,
        'group' => null
    );

    public function beforeFilter(){
        $this->Response->html();
        $this->Auth->authError = __('You do not have permission to access the page you just selected', true);
        $this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'dashboard');
        $this->Auth->logoutRedirect  = array('controller' => 'users', 'action' => 'home');
        $this->Auth->authorize = 'actions';
        //$this->Auth->actionPath = 'controllers/';
        $this->Auth->userScope = array('User.active' => '1');

        $this->Auth->autoRedirect = false;
        $this->Cookie->name = 'SME';

        if(!$this->Auth->user('id')){
            $cookie = $this->Cookie->read('User');
            if($cookie){
                $this->Auth->login($cookie);
            }
        }
        $this->current_user();
    }

    /* Retrieve current session user data */
    protected function current_user(){
        $this->user = $this->Session->read('Auth.User');
        if(!empty($this->user['id'])){
            $group = $this->Group->read(null, $this->user['group_id']);
            $this->user['name'] = $this->user['username'];
            $this->user['group'] = $group['Group']['name'];
            if($this->user['group'] == 'student'){
                $this->loadModel('Student');
                $student = $this->Student->findByNumber($this->user['name']);
                if(!empty($student['Student'])){
                    $this->user['name'] = $student['Student']['name'];
                    $this->user['student'] = $student['Student'];
                }
            }
        }else{
            $this->user = $this->default_user;
        }
        $this->set('user', $this->user);
    }

}
?>